
/**

team 任务执行器
 *
*/

import { CacheDataHostiles } from "GameLoop/WorldState/PollingStation/PollingTasks/RoomItem/CacheHostiles";
import { CacheDataStructure } from "GameLoop/WorldState/PollingStation/PollingTasks/RoomItem/CacheStructure";
import { State } from "framework/FSM/AState";
import { CreepAbilityHelper } from "utils/creep/CreepAbilityHelper";
import { BodySetup } from "utils/creep/CreepSetup";
import { UtilsHelper } from "utils/position/UtilsHelper";

export class DefendTaskExecute<T extends IStateArg<Room,ITask<ITeamTaskData<"defend">> >> extends State<T>
{

    public getActions(arg:Required<T>):Action[]
    {
         
        if(!arg.task)
        {
            return []
        }
        
        const hostcreeps  = arg.entity.getWorld().PollHostiles(arg.task.data.room);

        if(!hostcreeps)
        {
            return []
        }
        const room =  Game.rooms[arg.task.data.room] as Required<Room>;
        if(!room)
        {
            return []
        }
        if(hostcreeps.cacheData && hostcreeps.cacheData.length>0)
        {
            const cachestructures =   arg.entity.getWorld().PollStructure(arg.task.data.room);

            if(cachestructures)
            {
                // 
                const status = this.getEnemystatus(hostcreeps,cachestructures);
                if((status.sum_heal>=status.min_damage || status.sum_damage>2000) && status.enemy_count>0)
                {
                    // 
                    const lab = room.getEntity("LabEntity");
                    if(lab)
                    {
                        if(room.controller.level ==6)
                        {
                           
                            if(room.activeSafeMode() != OK)
                            {
                                // 主动主防
                            }
                        }
                        else
                        {
                            // 主动主防
                        }
                    }
                    else
                    {
                        // 没有lab
                        if(room.activeSafeMode() === OK)
                        {
                            // 没有lab则激活sf
                        }
                        else
                        {   
                            // 没有sf 就要请求其他房间援助
                        
                        }
                    }
                }
                else
                {
                    // 防御塔击杀
                }
            }
            else
            {
                // 没有建筑,激活低等级防御兵
            }
        }
        const actions:IActionCMD[]=[];
    
        return actions
    }

    private getEnemystatus(hostcreeps:CacheDataHostiles<ICacheHostileCreep>,cachestructures: CacheDataStructure<ICacheStructure> )
    {
        const firsthostile = hostcreeps.cacheData[0];
        let  towerdamage = cachestructures.TowerDamage[firsthostile.pos.x][firsthostile.pos.y];
        let min_damage = towerdamage;
        let sum_heals = 0;
        let hostiles_damage=0;
        let EnemyCount=0;
        const hostileCreeps=[];
        for(const item of hostcreeps.cacheData)
        {
            const creep = Game.getObjectById(item.id as Id<Creep>);
            if(creep)
            {
                towerdamage = cachestructures.TowerDamage[firsthostile.pos.x][firsthostile.pos.y]
                hostileCreeps.push(creep);
                const realDamage = CreepAbilityHelper.GetRealBearDamage(creep,towerdamage);
                if(realDamage<min_damage)
                {
                    min_damage =realDamage;
                }
                sum_heals+=  BodySetup.GetBodyAbility(HEAL,"heal","heal",creep.body);
                hostiles_damage+=CreepAbilityHelper.getCreepRangeAttackAbility(creep);
                hostiles_damage+=CreepAbilityHelper.getCreepAttackAbility(creep);
                if(!UtilsHelper.isNPC(creep))
                {
                    EnemyCount++;
                }
            }
        }
        return {
            sum_heal:sum_heals,         // 敌方单位治疗总量
            sum_damage:hostiles_damage, // 敌方单位伤害总量
            min_damage:min_damage,  // 打在敌方最肉的=单位的伤害
            enemy_count:EnemyCount,  // 非NPC的敌方单位数量
            hostileCreeps:hostileCreeps // 敌方的爬
        }
    }
}
